//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.elitecrm.webchat.robot;

import com.elitecrm.webchat.model.Message;
import com.elitecrm.webchat.model.Session;
import com.elitecrm.webchat.model.User;
import com.elitecrm.webchat.service.http.PoolingHttpService;
import com.elitecrm.webchat.util.StringUtil;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class SobotRobotEngine extends RobotEngine {
    private static final long serialVersionUID = -6083206522991817468L;


    // 机器人基础参数
    private String question;
    private String serverUrl;
    private JSONObject robotConfig;

    // EliteChat参数
    private long requestID;
    private long sessionID;
    private String clientID;
    private int queueID;
    private String in_Brand;
    private String in_From;

    public SobotRobotEngine(JSONObject recJSON, Session session) {

        // 机器人基础
        this.id = "BOT004";
        this.type = 4;
        this.robotEnginName = recJSON.has("name") ? recJSON.getString("name") : "智齿";
        this.serverUrl = recJSON.getString("serverUrl");

        // 机器人配置
        this.robotConfig = recJSON;

        // EliteChat参数赋值
        User client = session.getClient();
        this.requestID = session.getCurrentRequestId();
        this.sessionID = session.getId();
        this.queueID = session.getQueueId();
        this.clientID = client.getId();
        this.in_Brand = client.getBrand();
        this.in_From = client.getFrom();
    }

    // 机器人问题
    public String search(Message msg, long sessionId) {
        try {
            String question = msg.getMessage();
            RobotUtils.printLog("Call Robot question: " + question);

            String reqParam = this.getJSONBody(question);
            RobotUtils.printLog("Call Robot request: " + reqParam);

            Map<String, String> headers = new HashMap<>();
            String responseStr = PoolingHttpService.getInstance().postStr(this.serverUrl, headers, reqParam);
            RobotUtils.printLog("Call Robot result " + responseStr);
            return responseStr;
        } catch (Exception var9) {
            RobotUtils.printErrorLog(var9);
            return "";
        }
    }

    // 获取机器人调用报文
    public String getJSONBody(String question) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("action", "robot_search");
        jsonBody.put("requestID", this.requestID);
        jsonBody.put("sessionID", this.sessionID);
        jsonBody.put("queueID", this.queueID);
        jsonBody.put("clientID", this.clientID);
        jsonBody.put("in_Brand", this.in_Brand);
        jsonBody.put("in_From", this.in_From);
        jsonBody.put("robotConfig", this.robotConfig);

        // 机器人问题
        JSONObject dataJSON = new JSONObject();
        dataJSON.put("question", question);

        jsonBody.put("data", dataJSON);
        return jsonBody.toString();
    }

    // 机器人问题评分
    public String feedback(String feedback) {
        if (StringUtil.isNotNull(this.feedBackUrl)) {
            JSONObject jsonFeedback = JSONObject.fromObject(feedback);
            String status = jsonFeedback.getString("status");
            String docid = jsonFeedback.getString("docid");
            String docName = jsonFeedback.getString("docname");
            String uid = jsonFeedback.getString("uid");
            String cid = jsonFeedback.getString("cid");
            String msgid = jsonFeedback.getString("msgid");
            Map<String, String> headers = new HashMap<>();
            String params = this.getFeedBackJSONBody(status, docid, docName, msgid, uid, cid);
            RobotUtils.printLog("Call Robot feedback request: " + params);

            String responseStr = PoolingHttpService.getInstance().post(this.feedBackUrl, headers, params);
            RobotUtils.printLog("Call Robot feedback result: " + responseStr);
        }

        return null;
    }

    // 获取机器人问题评分调用报文
    public String getFeedBackJSONBody(String status, String docid, String docName, String msgid, String uid, String cid) {
        JSONObject jsonBody = new JSONObject();
        JSONObject dataJSON = new JSONObject();
        jsonBody.put("action", "robot_feedback");

        // 机器人问题评分
        dataJSON.put("uid", uid);
        dataJSON.put("cid", cid);
        dataJSON.put("docId", docid);
        dataJSON.put("docName", docName);
        dataJSON.put("status", status);
        dataJSON.put("msgId", msgid);

        jsonBody.put("data", dataJSON);
        return jsonBody.toString();
    }

    public JSONObject translateAnswer(String result) {
        if (StringUtil.isNotNull(result)) {
            JSONObject resultJSON = JSONObject.fromObject(result);
            String code = resultJSON.getString("code");
            if ("1000".equals(code)) {
                JSONObject data = resultJSON.getJSONObject("data");
                String answer = data.getString("answer");
                RobotUtils.printLog("答案是： " + answer);
            } else {
                RobotUtils.printLog("接口异常");
            }
        } else {
            RobotUtils.printLog("结果为空");
        }

        return null;
    }

    public String getAccessToken() {
        return "";
    }

    public String getQuestion() {
        return this.question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getServerUrl() {
        return this.serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public JSONObject getRobotConfig() {
        return robotConfig;
    }

    public void setRobotConfig(JSONObject robotConfig) {
        this.robotConfig = robotConfig;
    }

    public long getRequestID() {
        return requestID;
    }

    public void setRequestID(long requestID) {
        this.requestID = requestID;
    }

    public long getSessionID() {
        return sessionID;
    }

    public void setSessionID(long sessionID) {
        this.sessionID = sessionID;
    }

    public String getClientID() {
        return clientID;
    }

    public void setClientID(String clientID) {
        this.clientID = clientID;
    }

    public String getIn_Brand() {
        return in_Brand;
    }

    public void setIn_Brand(String in_Brand) {
        this.in_Brand = in_Brand;
    }

    public String getIn_From() {
        return in_From;
    }

    public void setIn_From(String in_From) {
        this.in_From = in_From;
    }

    public int getQueueID() {
        return queueID;
    }

    public void setQueueID(int queueID) {
        this.queueID = queueID;
    }
}
